/***MsMultiPartFormData By GYLite 2022-12-26
 * 用于打包multipart/form-data格式HTTP数据包的类  
*/
class MsMultiPartFormData  
{ 
	
	public boundary:string= "----WebKitFormBoundaryyXNeiZVoMucHiQc6";
	private _fieldName:string="Content-Disposition: form-data; name=\"XXXX\""; 
	private _fieldValue:string= "XXXX"; 
	private _fileField:string="Content-Disposition: form-data; name=\"XXXX\"; filename=\"XXXXXXXX\""; 
	private _fileContentType:string= "Content-Type: XXXX"; 
	private _formData:GYLite.GYByteArray;  

	public constructor() 
	{ 
		let s=this;
		s._formData=new GYLite.GYByteArray(); 
	}
	public append(key:string, value:any):void
	{
		let s= this;
		if(GYLite.CommonUtil.GYIs(value, egret.ByteArray))
		{			
			s.addFile(key,"blob", value, value["mime"]?value["mime"].type:"application/octet-stream");
		}			
		else
		s.addFormField(key, value);
	} 
	
	/** 
	 * 添加一个字段数据到From的数据包中 	 
	 */ 
	public addFormField(fieldName:string, fieldValue:string):void 
	{ 
		let s = this;
		var newFieldName:string=s._fieldName; 
		var newFieldValue:string=s._fieldValue; 
		
		newFieldName=newFieldName.replace("XXXX",fieldName); 
		newFieldValue=newFieldValue.replace("XXXX",fieldValue); 
		
		s._formData.writeUTFBytes("--"+s.boundary+"\r\n"); 
		s._formData.writeUTFBytes(newFieldName+"\r\n\r\n"); 
		s._formData.writeUTFBytes(newFieldValue+"\r\n"); 
	} 
	
	
	/** 
	 * 添加一个文件二进流数据到Form的数据包中，并指定二进流数据的类型 	 
	 * @param fieldName name属性
	 * @param fileName filename属性
	 * @param fileContent 文件字节数据
	 */ 
	public addFile(fieldName:string, fileName:string,fileContent:egret.ByteArray, contentType:string):void 
	{
		let s =this;
		var newFileField:String=s._fileField; 
		var newFileContentType:String=s._fileContentType; 
		
		newFileField=newFileField.replace("XXXX",fieldName); 
		newFileField=newFileField.replace("XXXXXXXX",fileName); 
		
		newFileContentType=newFileContentType.replace("XXXX",contentType); 
		
		s._formData.writeUTFBytes("--"+s.boundary+"\r\n"); 
		s._formData.writeUTFBytes(newFileField+"\r\n"); 
		s._formData.writeUTFBytes(newFileContentType+"\r\n\r\n"); 
		
		s._formData.writeBytes(fileContent,0,fileContent.length); 
		
		s._formData.writeUTFBytes("\r\n"); 
	} 
	
	/** 
	 * 添加一个文件二进流数据到Form的数据包中 	 
	 */ 
	public addStreamFile(fieldName:string, fileName:string,fileContent:GYLite.GYByteArray,mimeType:string="application/octet-stream"):void 
	{ 
		this.addFile(fieldName, fileName, fileContent, mimeType); 
	} 
	
	/** 
	 * 把Form中所有的字段与二进制流数据打包成一个完整的Form数据包 	 
	 */ 
	public prepareFormData():void 
	{
		let s =this;
		s._formData.writeUTFBytes("--" + s.boundary + "--\r\n"); 
	} 
	
	/** 
	 * 获得From的完整数据 	 
	 */ 
	public getFormData():ArrayBuffer
	{
		let s = this;
		return s._formData.buffer; 
	}
	public getFormDataBytes():GYLite.GYByteArray 
	{
		let s = this;
		return s._formData; 
	}
	
}